package com.ts.api.config;

import com.auth0.jwt.algorithms.Algorithm;
import com.gitee.apanlh.util.algorithm.signature.JwtUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.concurrent.TimeUnit;

/**
 *  token配置
 */
@Configuration
@ConfigurationProperties(prefix = "api.token")
public class TokenConfig {

    /** 密钥 */
    private String key;
    /** 默认单位秒 */
    private Long expireTime;
    /** 单位 */
    private TimeUnit unit;
    /** 算法 */
    private Algorithm algorithm;
    /** 参数加密密钥 */
    private String paramKey;
    /** 参数加密IV */
    private String paramIv;

    public TokenConfig() {
        super();
    }

    public TokenConfig(String key, Long expireTime, TimeUnit unit) {
        this.key = key;
        this.expireTime = expireTime;
        this.unit = unit;
    }

    @PostConstruct
    public void initAlgorithm() {
        this.algorithm = JwtUtils.createHmac256(key);
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Long getExpireTimeToMillis() {
        return this.unit.toMillis(expireTime);
    }

    public Long getExpireTimeToSeconds() {
        return this.unit.toSeconds(expireTime);
    }

    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }

    public TimeUnit getUnit() {
        return unit;
    }

    public void setUnit(TimeUnit unit) {
        this.unit = unit;
    }

    public Algorithm getAlgorithm() {
        return algorithm;
    }

    public String getParamKey() {
        return paramKey;
    }
    public void setParamKey(String paramKey) {
        this.paramKey = paramKey;
    }

    public String getParamIv() {
        return paramIv;
    }

    public void setParamIv(String paramIv) {
        this.paramIv = paramIv;
    }
}
